//proxy 代理 13个方法  参数一摸一样
//Reflect 反射 13个方法 参数一模一样
//mobx observable  类似vuex的观察者模式

let person111 = {
    name:'小满',
    age:18
}
//proxy支持对象 数组 函数 set mep

let personProxy = new Proxy(person111,{
    // 取值
    get(target: { name: string; age: number }, p: PropertyKey, receiver: any): any {

},
    //赋值
    set(target: { name: string; age: number }, p: PropertyKey, value: any, receiver: any): boolean {
    return  true
},
    //拦截函数调用
    apply(target: { name: string; age: number }, thisArg: any, argArray?: any): any {

    },
    //拦截in操作符
    has(target: { name: string; age: number }, p: PropertyKey): boolean {
        return  true
    },
    //拦截for in的
    // ownKeys(target: { name: string; age: number }): PropertyKey[] {
    //     return []
    // },
    // 拦截new操作符的
    construct(target: { name: string; age: number }, argArray: any, newTarget?: any): object {
        return  {}
    },
    // 拦截删除的操作
    defineProperty(target: { name: string; age: number }, p: PropertyKey, attributes: PropertyDescriptor): boolean {
        return true
    }
})
let personProxy1 = new Proxy(person111,{
    get(target: { name: string; age: number }, p: PropertyKey, receiver: any): any {
     if (target.age <= 18){
         return Reflect.get(target,p,receiver)
     }else{
         return '小满成年啦'
     }
    }
})
// console.log(personProxy1.age)


/**
 * 用Proxy实现观察者模式(没实现)
 */
//事件存储器
const list:Set<Function> = new Set()
//订阅函数
const autoRun = (cb:Function) => {
    if (list.has(cb)){
        list.add(cb)
    }
}
const observable = <T extends object>(params:T) => {
    return new Proxy(params,{
        set(target: T, p: PropertyKey, value: any, receiver: any): boolean {
            const result = Reflect.set(target,p,value,receiver)
            list.forEach(fn => fn())
            return  result
        }
    })
}
const personProxy2 = observable({name:'周老板',attr:'威猛先生'})
autoRun(() => {
    console.log('有变化啦')
})
personProxy2.name= '小周'
